// Learn cc.Class:
//  - [Chinese] http://www.cocos.com/docs/creator/scripting/class.html
//  - [English] http://www.cocos2d-x.org/docs/editors_and_tools/creator-chapters/scripting/class/index.html
// Learn Attribute:
//  - [Chinese] http://www.cocos.com/docs/creator/scripting/reference/attributes.html
//  - [English] http://www.cocos2d-x.org/docs/editors_and_tools/creator-chapters/scripting/reference/attributes/index.html
// Learn life-cycle callbacks:
//  - [Chinese] http://www.cocos.com/docs/creator/scripting/life-cycle-callbacks.html
//  - [English] http://www.cocos2d-x.org/docs/editors_and_tools/creator-chapters/scripting/life-cycle-callbacks/index.html

/**
 * 设置界面
 */
cc.Class({
    extends: cc.Component,

    properties: {
        aboutPrefab: cc.Prefab, //关于、服务协议(共用预制)
        modifyPwdPrefab: cc.Prefab,  //修改密码预制 (提现密码，登录密码)
        bindBankPrefab: cc.Prefab,  //添加银行卡预制
        _resultStr: null,
        _resultStr1: null,
    },

    // LIFE-CYCLE CALLBACKS:

    // onLoad () {},

    start () {
        cc.loader.loadRes('/text/protoServe', function (error, result) {
            if(error){ return }
            this._resultStr =result;
        }.bind(this));
        cc.loader.loadRes('/text/proto', function (error, result) {
            if(error){ return }
            this._resultStr1 =result;
        }.bind(this));
    },

    //我的银行卡
    onBankCardBtn:function(){
        var canvas = cc.find("Canvas");
        var bindBankPrefab = cc.instantiate(this.bindBankPrefab);
        bindBankPrefab.getComponent(bindBankPrefab.name).init(2,null);
        canvas.addChild(bindBankPrefab);
        
    },

    //修改提现密码
    onCashPasswordBtn: function(){
        if(User.getIsbindtel())  //是否绑定手机号  
        {
            var canvas = cc.find("Canvas");
            var modifyPwdPrefab = cc.instantiate(this.modifyPwdPrefab);
            modifyPwdPrefab.getComponent(modifyPwdPrefab.name).init(2);
            canvas.addChild(modifyPwdPrefab);
        }
        else
        {
            var canvas = cc.find("Canvas");
            var verifiedPrefab = cc.instantiate(this.modifyPwdPrefab);
            verifiedPrefab.getComponent(verifiedPrefab.name).init(2);
            canvas.addChild(verifiedPrefab);
        }
    },

    //修改登录密码
    onLoginPasswordBtn:function(){
        if(User.getIsbindtel())  //是否绑定手机号
        {
            var canvas = cc.find("Canvas");
            var modifyLoginPrefab = cc.instantiate(this.modifyPwdPrefab);
            modifyLoginPrefab.getComponent(modifyLoginPrefab.name).init(1);
            canvas.addChild(modifyLoginPrefab);
        }
        else
        {
            var canvas = cc.find("Canvas");
            var verifiedPrefab = cc.instantiate(this.modifyPwdPrefab);
            verifiedPrefab.getComponent(verifiedPrefab.name).init(1);
            canvas.addChild(verifiedPrefab);
        }
    },

    //音效
    onVoice: function(toggle, customEventData){
        if(toggle.getComponent(cc.Toggle).isChecked)
        {
            CL.SOUND.set_effect_mute(false);
            ComponentsUtils.showTips("开启声音");
        }
        else
        {
            CL.SOUND.set_effect_mute(true);
            ComponentsUtils.showTips("关闭声音");
        }
    },

    //服务协议
    onServe: function(){
        var canvas = cc.find("Canvas");
        var severPrefab = cc.instantiate(this.aboutPrefab);
        severPrefab.getComponent(severPrefab.name).init('服务协议',this._resultStr1);
        canvas.addChild(severPrefab);
    },

    //关于
    onAbout: function(){
        var canvas = cc.find("Canvas");
        var aboutPrefab = cc.instantiate(this.aboutPrefab);
        aboutPrefab.getComponent(aboutPrefab.name).init('关于',this._resultStr);
        canvas.addChild(aboutPrefab);
    },

    //退出账号
    onQuit: function(){
        var recv = function recv(ret) { 
            ComponentsUtils.unblock();
            if(ret.Code == 0 )
            {
                User.setIsvermify(false);
                User.upDateUserInfo(true,"");
                User.setLoginToken(ret.Token);
               
                var callback = function callback(ret) {
                    if(ret && ret == true)//刷新用户信息
                    {
                       // CL.MAINMANAGE.onLoginSK();
                        window.Notification.emit("updateUserUI","");
                    }
                    else//返回大厅
                    {
                        CL.MAINMANAGE.onTgSelect(null,'0');
                    }
					CL.MAINMANAGE.onLoginSK();
                    this.onClose();
                }
                CL.MAINMANAGE.goToLogin(callback.bind(this));
            }
            else
            {
                this.getOneToken();
            }
			
        }.bind(this);

        var data = {
            Token:User.getLoginToken(),
            UserCode:User.getUserCode(),
            Equipment:Utils.getEquipment(),
        };
        CL.HTTP.sendRequest(DEFINE.HTTP_MESSAGE.OUTACCOUNT, data, recv.bind(this),"POST");    
        ComponentsUtils.block();
    },

    //获取一个临时token
    getOneToken:function(){
        var recv = function recv(ret) { 
            ComponentsUtils.unblock();
            if(ret.Code == 0 )
            {
                User.setIsvermify(false);
                User.upDateUserInfo(true,"");
                User.setLoginToken(ret.Token);

                var callback = function callback(ret) {
                    if(ret && ret == true)
                    {
                        
                        window.Notification.emit("updateUserUI","");
                        return; 
                    }
                    else
                    {
                        CL.MAINMANAGE.onTgSelect(null,'0');
                    }
					CL.MAINMANAGE.onLoginSK();
                    this.onClose();
                }
                CL.MAINMANAGE.goToLogin(callback.bind(this));
            }
            else
            {
                ComponentsUtils.showTips(ret.Msg);
                this.onClose();
            }
        }.bind(this);

        var data = {
            Equipment:Utils.getEquipment(),
        };
        CL.HTTP.sendRequest(DEFINE.HTTP_MESSAGE.GETTOKEN, data, recv.bind(this),"POST");    
        ComponentsUtils.block();
    },

    onClose: function(){
        this.node.getComponent("Page").backAndRemove();
    },

    // update (dt) {},
});
